package com.coscon.utils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/*
 * AES算法，
 * 采用CBC算法模式
 * PKCS5/PKCS7填充模式
 */
public class AESUtil {
	
	private String vi;
	
	//构造函数，参数为加密向量
	public AESUtil(String vi){
			this.vi = vi;
	}
	/** 将二进制转换成16进制 2. 
	 * * @param buf 3.
	 *  * @return 4. 
	 *  */
	public  String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}
	/*
	 * AES解密
	 * 参数一：数据
	 * 参数二：秘钥
	 */
	public  String decrypt(byte[] sSrc, String sKey) {
		try {
			byte[] raw = sKey.getBytes();
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			IvParameterSpec iv = new IvParameterSpec(vi.getBytes());
			cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
			try {
				byte[] original = cipher.doFinal(sSrc);
				String originalString = new String(original,"UTF-8");
//				String originalString = parseByte2HexStr(original);
				return originalString;
			} catch (Exception e) {
				System.out.println(e.toString());
				return null;
			}
		} catch (Exception ex) {
			System.out.println(ex.toString());
			return null;
		}
	}
	/*
	 * AES加密
	 * 参数一：数据
	 * 参数二：秘钥
	 */	
	public  byte[] encrypt(String content, String password) throws IllegalBlockSizeException, BadPaddingException,InvalidAlgorithmParameterException, UnsupportedEncodingException {
		try {
			byte[] raw = password.getBytes();
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "算法/模式/补码方式"
			IvParameterSpec iv = new IvParameterSpec(vi.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
			byte[] result = cipher.doFinal(content.getBytes("UTF-8"));
			return result; // 加密
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		}
		return null;
	}
	
    /**
     * Convert hex string to byte[]
     * @param hexString the hex string
     * @return byte[]
     */
    public byte[] hexStringToBytes(String hexString) {
        if (null == hexString || "".equals(hexString)) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
    /**
     * Convert char to byte
     * @param c char
     * @return byte
     */
     private byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
     }
     
     /*
	public static void main(String[] args) throws IllegalBlockSizeException,BadPaddingException, InvalidAlgorithmParameterException {
		long start = System.currentTimeMillis();
//		AESUtil aes= new AESUtil("chinashanghaijky");
//		byte[] miByte = aes.encrypt("hello,world!", "chinashanghaijky");
//		String str = aes.parseByte2HexStr(miByte);
//		System.out.println("密文十六进制：" + str + ",长度：" + str.length());
//		String mingStr  = aes.Decrypt(miByte,"chinashanghaijky");
//		System.out.println("明文：" + mingStr);
//		float a = 222222222;
//		System.out.println(NumberUtils.t);
		
//		String s = "3.05928986E9";
//		float b = NumberUtils.toFloat(s);
//		System.out.println(b*2);
//		
//		
		
//		byte[] b = new byte[2];
//		b[0] = 0x0E;
//		System.out.println(b[0]);
		
		String str = "14";
		
		System.out.println(StringUtils.indexOf("进线1", "进3线"));
		System.out.println(StringUtils.contains("进线1", "进线"));
		
		
		
		long end = System.currentTimeMillis();
		System.out.println("总时间：" + (end-start));
	}
	*/


}
